home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / VideoToolboxSources / GDVideo.c < prev    next >
Encoding:
Text File  |  1994-11-02  |  54.1 KB  |  1,498 lines  |  [TEXT/KAHL]

  1. /* 
  2. GDVideo.c
  3. Copyright © 1989-1994 Denis G. Pelli
  4.  
  5. Complete set of routines to control video drivers directly, bypassing
  6. QuickDraw's Color and Palette Managers. There is a separate function call for
  7. each of the Control and Status calls implemented by video drivers. They are
  8. described in Designing Cards and Drivers, 3rd Ed., chapter 9.
  9.  
  10. This file also contains several other helpful routines that deal with video
  11. device drivers. For background, read “Video synch”.
  12.  
  13. THE MORE-USEFUL (HIGH-LEVEL) ROUTINES: (in alphabetical order)
  14.  
  15. char *GDCardName(GDHandle device);
  16. Returns the address of a C string containing the name of the video card. You
  17. should call DisposePtr() on the returned string when you no longer need it.
  18. Takes about 1.5 ms because Apple's slot routines are very slow. 
  19.  
  20. short GDClutSize(GDHandle device);
  21. Returns the number of entries in the video driver's clut in the current video
  22. mode (i.e. current pixel size). VideoToolbox.h defines a preprocessor macro
  23. GDCLUTSIZE(device) that expands to equivalent inline code.
  24.  
  25. long GDColors(GDHandle device)
  26. Number of colors, in the current mode.
  27.  
  28. short GDDacSize(GDHandle device)
  29. Figures out how many bits in the video dac.
  30.  
  31. OSErr GDGetEntries(GDHandle device,short start,short count,ColorSpec *table);
  32. This is much as you'd expect after reading GDSetEntries above. Note that unless
  33. the gamma table is linear, the values returned may not be the same as those
  34. originally passed by GDSetEntries. So call GDUncorrectedGamma first. Try the demo 
  35. TimeVideo.
  36.  
  37. OSErr GDGetGamma(GDHandle device,GammaTbl **myGammaTblHandle);
  38. Returns a pointer to the Gamma table in the specified video device. (I.e. you
  39. pass it a pointer to your pointer, a handle, which it uses to load your
  40. pointer.)
  41.  
  42. OSErr GDGetPageCnt(GDHandle device,short mode,short *pagesPtr);
  43. Called "GetPages" in Designing Cards and Drivers, 3rd Ed. You tell it which mode
  44. you're interested in. It tells you how many pages of video ram are available in 
  45. that mode.
  46.  
  47. Boolean GDHasMode(GDHandle device,short mode,short *pixelSizePtr,short *pagesPtr);
  48. Returns 0 if no such video mode, returns 1 if mode is available.
  49. If pixelSizePtr is not NULL, then sets *pixelSizePtr to pixelSize or -1 if unknown.
  50. If pagesPtr is not NULL, then sets *pagesPtr to pages.
  51.  
  52. unsigned char *GDName(GDHandle device);
  53. Returns a pointer to the name of the driver (a pascal string). This is quick.
  54. The string is part of the driver itself, so don't modify it or try to dispose of it.
  55.  
  56. unsigned char *GDNameStr(GDHandle device);
  57. Returns a pointer to the name of the driver, as a C string. 
  58. The string is static, and will be overwritten by the next call to GDNameStr().
  59.  
  60. ColorSpec *GDNewLinearColorTable(GDHandle device);
  61. Creates a default table for use when gdType==directType.
  62.  
  63. OSErr GDPrintGammaTable(FILE *o,GDHandle device);
  64.  
  65. OSErr GDRestoreDeviceClut(GDHandle device);
  66. Nominally equivalent to Apple's RestoreDeviceClut(), which is only available
  67. since System 6.05. However, I find that Apple's routine sometimes does nothing,
  68. whereas this routine always works. Passing a NULL argument causes restoration of
  69. cluts of all video devices.
  70.  
  71. OSErr GDSaveGamma(GDHandle device);
  72. OSErr GDRestoreGamma(GDHandle device);
  73. Use an internal cache to save and restore the device's gamma-correction table. Call
  74. GDRestoreGamma before GDRestoreDeviceClut.
  75.  
  76. OSErr GDSetEntriesByType(GDHandle device,short start,short count,ColorSpec *table);
  77. Checks the (**device).gdType field and calls GDSetEntries, GDDirectSetEntries,
  78. or nothing, as appropriate.
  79.  
  80. OSErr GDSetEntriesByTypeHighPriority(GDHandle device,short start,short count
  81.     ,ColorSpec *table);
  82. Calls GDSetEntriesByType() while the processor priority has been temporarily 
  83. raised to 7.
  84.  
  85. OSErr GDSetGamma(GDHandle device, GammaTbl *gamma);
  86. Loads a Gamma table into the specified video device. The video driver will make
  87. a copy of your table. You can discard your table after making this call. Note
  88. that the video driver only uses the gamma table when performing SetEntries, i.e.
  89. when actually loading the clut. The structure of the gamma table is (finally!)
  90. documented in Designing Cards and Drivers, 3rd edition, pages 215-216. Beware of
  91. a discrepancy between the documentation and the definition in QuickDraw.h:
  92. gFormulaData is described as a byte array in the text, but is declared as a
  93. short array in the QuickDraw.h header file. NOTE: a few video drivers (Radius
  94. PowerView and SuperMac ColorCard) do not support gamma tables. The SuperMac
  95. ColorCard is well behaved, giving the appropriate error code, returned by
  96. GDSetGamma and GDGetGamma. The Radius PowerView reports no error yet ignores the
  97. GDSetGamma call and returns a table full of zeroes in response to GDGetGamma.
  98. See NOTE from Tom Busey below. However, if all you want to do is call
  99. GDUncorrectedGamma, then these limitations won't affect you, because the drivers
  100. that lack gamma tables always give you precisely the behaviour that one requests
  101. by calling GDUncorrectedGamma.
  102.  
  103. OSErr GDSetPageDrawn(GDHandle device,short page);
  104. Choose which page of video memory will be used by future drawing operations. 
  105. Untested.
  106.  
  107. OSErr GDSetPageShown(GDHandle device,short page);
  108. Choose which page of video memory we see. Untested.
  109.  
  110. OSErr GDUncorrectedGamma(GDHandle device);
  111. Asks GDSetGamma to load a linear gamma table, i.e. no correction. (The gamma
  112. correction implemented by table-lookup in the video driver is too crude for
  113. experiments that want accurate luminance control.)
  114.  
  115. int GDVersion(GDHandle device)
  116. Returns the version number of the driver. From the first word-aligned word after
  117. the name string. This is quick.
  118.  
  119. LESS-USEFUL (LOW LEVEL) ROUTINES:
  120.  
  121. OSErr GDControl(int refNum,int csCode,Ptr csParamPtr)
  122. Uses low-level PBControl() call to implement a "Control()" call that works! 
  123. I don't know why this wasn't discussed in Apple Tech Note 262.
  124.  
  125. OSErr GDDirectSetEntries(GDHandle device,short start,short count,ColorSpec *table);
  126. If your pixel depth is >8 then the setEntries call is disabled, and you must use
  127. this instead of GDSetEntries().
  128.  
  129. VideoDriver *GDDriverAddress(GDHandle device);
  130. Returns a pointer to the driver, whether in ROM or RAM. Neither this prototype
  131. nor the definition of the VideoDriver structure are included in VideoToolbox.h.
  132.  
  133. OSErr GDGetDefaultMode(GDHandle device,short *modePtr)
  134. It tells you what the default mode is. I'm not sure what this means.
  135.  
  136. OSErr GDGetGray(GDHandle device,Boolean *flagPtr);
  137. Get gray flag. 0 for color. 1 if all colors mapped to luminance-equivalent gray tones.
  138.  
  139. OSErr GDGetInterrupt(GDHandle device,Boolean *flagPtr);
  140. Get flag. 1 if VBL interrupts of this card are enabled. 0 if disabled. 
  141.  
  142. OSErr GDGetMode(GDHandle device,short *modePtr,short *pagePtr,Ptr *baseAddrPtr);
  143. It tells you the current mode, page of video memory, and the base address of that
  144. page.
  145.  
  146. OSErr GDGetPageBase(GDHandle device,short page,Ptr *baseAddrPtr);
  147. Called "GetBaseAddr" in Designing Cards and Drivers, 3rd Ed. You tell it which
  148. page of video memory you're interested in (in the current video mode). It tells
  149. you the base address of that page.
  150.  
  151. OSErr GDGrayPage(GDHandle device,short page);
  152. Called "GrayScreen" in Designing Cards and Drivers, 3rd Ed. Fills the specified
  153. page with gray, i.e. the dithered desktop pattern. I'm not aware of any
  154. particular advantage in using this instead of FillRect(). Designing Cards and
  155. Drivers, 3rd Edition, Chapter 9, says that for direct devices, i.e. >8 bit
  156. pixels, the driver will also load a linear, gamma-corrected, gray color table.
  157.  
  158. OSErr GDReset(GDHandle device, short *modePtr, short *pagePtr, Ptr *baseAddrPtr);
  159. Initialize the video card to its startup state, usually 1 bit per pixel. Returns
  160. the parameters of that state.
  161.  
  162. OSErr GDSetDefaultMode(GDHandle device,short mode);
  163. Supposedly, you tell it what mode you want to start up with when you reboot.
  164. (I've never been able to get this to work. No error and no effect. Perhaps I've
  165. misunderstood its purpose.)
  166.  
  167. OSErr GDSetEntries(GDHandle device,short start,short count,ColorSpec *table);
  168. Does a low-level setEntries Control call to the video card's driver, loading any
  169. number of clut entries with arbitrary rgb triplets. (Note that the driver will
  170. transform your rgb triplets via the gamma table before loading them into the
  171. clut; so call GDUncorrectedGamma first.) "device" specifies which video device's
  172. clut you want to load. "start" is either in the range 0 to clutSize-1,
  173. indicating which clut entry to load first (in "sequence mode"), or is -1
  174. (requesting "index mode"). "count" is the number of entries to be modified,
  175. minus 1. "table" is a ColorSpec array (not a ColorTable) containing the rgb
  176. triplets that you want to load into the clut. In sequence mode "start" must be
  177. in the range 0 to clutSize-1, the i-th element of table corresponds to the
  178. i+start entry in the clut, and the "value" field of each element of table is
  179. ignored. In index mode "start" must be -1, and the "value" field of each element
  180. of table indicates which clut entry to load it into. The arguments start, count,
  181. and table are the same as for the Color Manager call SetEntries(), documented in
  182. Inside Macintosh V-143. (Most drivers wait for blanking before modifying the
  183. clut. For a full discussion, read the VideoToolbox "Video synch" file.) You may
  184. also want to look at the file SetEntriesQuickly.c, which provides the
  185. functionality of GDSetEntries and GDDirectSetEntries, but bypasses the video
  186. driver to access the hardware directly.
  187.  
  188. OSErr GDSetGray(GDHandle device,Boolean flag);
  189. Set gray flag. 0 for color. 1 if all colors mapped to luminance-equivalent gray tones.
  190.  
  191. OSErr GDSetInterrupt(GDHandle device,Boolean flag);
  192. Set flag to 1 to enable VBL interrupts of this card, or 0 to disable. 
  193.  
  194. OSErr GDSetMode(GDHandle device,short mode,short page,Ptr *baseAddrPtr);
  195. You tell it what mode and video page you want. It sets 'em and returns the base
  196. address of that page in video memory. Also, because Apple said so, the video
  197. driver sets the entire clut to 50% gray. Note that this changes things behind
  198. QuickDraw's back. For most applications life will be simpler if you overtly ask
  199. QuickDraw to take charge by calling Apple's SetDepth() instead of GDSetMode().
  200. WARNING: Apple now considers the mode numbers merely ordinal. E.g. on the
  201. 8•24 video card the 32-bit mode has mode number 0x84, not 0x85 as you might
  202. have expected on the basis of old Apple documentation and other Apple video cards.
  203.  
  204. OSErr GDStatus(int refNum,int csCode,Ptr csParamPtr)
  205. Uses low-level PBStatus() call to implement a "Status()" call that works! The
  206. need for this is explained in Apple Tech Note 262, which was issued in response
  207. to my bug report in summer of '89.
  208.  
  209. PatchMacIIciVideoDriver();
  210. Fixes a bug in the Mac IIci built-in video driver that would crash GDGetEntries.
  211. The patch persists until reboot. It is unlikely that you will need to call this
  212. explicitly, PatchMacIIciVideoDriver() is automatically invoked the first time
  213. GDGetEntries is called. The Mac IIci built-in video driver
  214. (.Display_Video_Apple_RBV1 driver, version 0) has a bug that causes it to crash
  215. if you try to do a GetEntries Status request. PatchMacIIciVideoDriver() finds
  216. and patches the copy of the buggy driver residing in memory. (If the driver is
  217. ROM-based it first moves it to RAM, and then patches that.) Only two
  218. instructions are modified, to save & restore more registers. This fix persists
  219. only until the next reboot. If the patch is successfully applied the version
  220. number is increased by 100.
  221.  
  222. NOTES:
  223.  
  224. Several bugs in version 2 (in System ≤6.03) of the video driver for Apple's
  225. "Toby Frame Buffer" video card that affected use of the GetGamma call were
  226. fixed in version 3 (in System 6.04), apparently in response to my bug report to
  227. Apple.
  228.  
  229. The control/status-call parameter in a GDControl or GDStatus call specifies what
  230. you want done. See Designing Cards and Drivers, 3rd Edition, Chapter 9. Note
  231. that sometime around 1990 Apple renamed some of the Control and Status calls,
  232. giving them names that better reflect their function. I followed suit. However,
  233. Apple neglected to update their book, Designing Cards and Drivers, now in its
  234. 3rd edition. Fortunately, they define both names in their Video.h header file.
  235. To avoid confusion, here are the equivalences. Note that "csc" stands for 
  236. "Control and Status Call"
  237. Control call:
  238.  cscGrayPage =  cscGrayScreen = 5
  239. Status calls:
  240.  cscGetPageCnt = cscGetPages = 4
  241.  cscGetPageBase = cscGetBaseAddr = 5
  242.  
  243. Based on:
  244. Inside Macintosh-II (Device Manager)
  245. Designing Cards and Drivers, 3rd Edition, Chapter 9.
  246. Tech Note 262: "Controlling Status Calls"
  247. "GreyScale Information" from AppleLink "Apple Technical Info"
  248. "Video Configuration ROM Software Specification" also from AppleLink,
  249. in "Developer Tech Support:Macintosh:32 Bit QuickDraw"
  250.  
  251. NOTE:
  252. Tom Busey (busey@indiana.edu) writes: "I'm using an 8-bit SuperMac ColorCard
  253. that a used computer dealer gave me when I ordered a Toby card. I discovered
  254. that the status and control calls for GDUncorrectedGamma return -17 and -18
  255. respectively, which means that the driver doesn't support different gammas.
  256. GDUncorrectedGamma returns an error message, but GDOpenWindow doesn't use the
  257. error message or pass it back to the calling program. I'm wondering if there are
  258. people using GDOpenWindow who think that they are getting a linear gamma but who
  259. are actually getting an error message and not learning about it."
  260.      Tom's facts are right, but he needn't worry. A few video drivers, e.g.
  261. Radius PowerView, and apparently the SuperMac ColorCard, don't implement gamma
  262. tables at all, but the error from GDUncorrectedGamma is probably not a concern.
  263. The real test is to run TimeVideo. TimeVideo does a write-and-read-back test of
  264. the clut of your video card. If that test passes then you can safely conclude
  265. that there's no gamma translation going on. So, as nearly every document in the
  266. VideoToolbox says: run TimeVideo and read the report.
  267.      Here's how gamma tables work. According to the Apple manual (Designing
  268. Cards and Drivers), the values in the rgb triplets that you supply in a
  269. setentries call to the video driver are first translated via the gamma table
  270. (each r,g, and b component separately) before being loaded into the CLUT
  271. hardware table. Most drivers maintain a gamma table internally (in computer
  272. memory) and allow you to get and set it via the GDGetGamma and GDSetGamma calls.
  273. A few video drivers (Radius PowerView, SuperMac ColorCard) don't support gamma
  274. tables. They load your rgb values directly into the CLUT, without translation.
  275. However, for users of the VideoToolbox that's usually fine, since that's exactly
  276. the behavior that we routinely request by calling GDUncorrectedGamma. Of course,
  277. if you want to load a custom gamma table, other than the identity
  278. transformation, then you'll be calling GDSetGamma, and you should make sure it
  279. does not return an error. (Alas, the Radius PowerView driver more or less
  280. ignores the set- and get-gamma requests--GDGetGamma returns a table that's all
  281. zeroes--but the driver fails to report an error. I wrote to the Radius
  282. programmer a year ago, but they're no longer interested in working on that
  283. product.)
  284.  
  285. HISTORY:
  286. 9/29/89     dgp    added caution above that successive calls to SetEntries may be on one 
  287.                 frame.
  288. 11/21/89    dgp    corrected mode list: 0x80... as pointed out by Chuck Stein
  289. 11/30/89    dgp    added note above from Don Kelly.
  290.                 Added cautionary note above about GDSetEntries().
  291. 3/1/90        dgp    updated comments.
  292. 3/3/90        dgp    included Video.h instead of defining VDSetEntryRecord and VDPageInfo.
  293. 3/20/90        dgp    made compatible with MPW C.
  294. 3/23/90        dgp    changed char to unsigned char in VDDefModeRec
  295.                 and VDFlagRec to prevent undesirable sign extension.
  296. 4/2/90        dgp    Deleted mode argument from GDGrayScreen().
  297. 7/28/90        dgp Fixed stack overflow in GDGrayScreen, by declaring SysEnvRec static.
  298. 10/20/90    dgp    Apple has renamed the control and status calls, so I followed suit:
  299.                 •GDGetPageBase replaces GDGetBaseAddr
  300.                 •GDReset replaces GDInit
  301.                 •GDGrayPage replace GDGrayScreen
  302.                 •GDGetPageCnt replaces GDGetPages
  303. 2/12/91        dgp Discovered that the old bug in GDGrayPage is still present in System
  304.                 6.05, so I removed the conditional around the bug fix. TestGDVideo
  305.                 now works with: Mac II Video Card, Mac II Display Card (4•8), 
  306.                 Mac IIci Built-in Video, TrueVision NuVista, Mac IIsi Built-in Video.
  307. 7/22/91        dgp    Changed definition of csGTable from GammaTblPtr to Ptr, 
  308.                 to conform with MPW C.
  309. 8/24/91        dgp    Made compatible with THINK C 5.0.
  310. 10/22/91    dgp    With help from Bart Farell, converted all functions headers to
  311.                 Standard C style.
  312. 8/26/92        dgp    added a few miscellaneous comments
  313.                 In all routines, *baseAddrPtr is now set only if baseAddrPtr is not NULL.
  314. 10/10/92    dgp    Added GDRestoreDeviceClut(). Removed obsolete support for THINK C 4.
  315. 11/30/92    dgp corrected error in comment documenting values of argument to GDSetGray().
  316.                 Set flag to zero for color,1 for luminance-mapped gray.
  317. 12/9/92        dgp    Enhanced GDRestoreDeviceClut(). Passing a NULL argument now requests
  318.                 application to all devices. I only just learned that Apple's
  319.                 RestoreDeviceClut() behaves in this way.
  320. 12/15/92    dgp Renamed GDRestoreDeviceClut to GDRestoreDeviceClut to be consistent
  321.                 with Apple's capitalization of RestoreDeviceClut.
  322. 12/16/92    dgp Updated comments to be consistent with 3rd edition of Designing Cards
  323.                 and Drivers. •Renamed myGDHandle to "device", which is easier to read.
  324.                 Renamed GDLinearGamma to GDUncorrectedGamma, and generalized it
  325.                 to work with any size DAC. For compatibility with old programs
  326.                 VideoToolbox.h now has a #define statement making GDLinearGamma
  327.                 an alias for GDUncorrectedGamma.
  328. 12/30/92    dgp Updated some of the comments. Eliminated Files.h.
  329. 12/30/92    dgp Use GDClutSize() to determine clut size.
  330. 1/4/93        dgp In GDClutSize, check for fixedType.
  331. 1/5/93        dgp In GDClutSize, only set last clut entry.
  332.                 Added PatchMacIIciVideoDriver() to the end of this file, and 
  333.                 automatically invoke it the first time GDGetEntries is called.
  334. 1/6/93        dgp    Cleaned up GDClutSize. It now seems to work correctly in the direct modes.
  335. 1/18/93    dgp    Spruced up the documentation.
  336.             •Added all the code formerly in GDIdentify.c, but omitted the obsolete 
  337.             function GDIdentify(), which simply printed GDName() and GDVersion.
  338.             •Enhanced GDGetEntries() to avoid calling drivers that are known
  339.             to crash, returning a statusErr instead.
  340.             •Recoded PatchMacIIciVideoDriver() so as not to call GetScreenDevice.c.
  341. 2/1/93    dgp    Fixed endless loop in PatchMacIIciVideoDriver. Enhanced to deal with
  342.             ROM-based drivers as well.
  343. 2/5/93    dgp    Expanded the documentation of GDSetEntries above, and supplied sample
  344.             code showing how to load the clut.
  345. 2/7/93    dgp Fixed endless loop in PatchMacIIciVideoDriver.
  346. 2/20/93    dgp    Fixed bug in GDUncorrectedGamma() that was causing TestCluts to fail
  347.             for video devices that have nonstandard gamma tables. 
  348. 3/18/93    dgp    Fixed divide by zero error in GDClutSize.
  349. 4/6/93    dgp    GDGetPageCnt() now sets *pagePtr argument only if no error occurs.
  350.             Deleted GDModeName(). Removed assumption, in all routines, that there
  351.             is any particular correspondence between the video mode number and
  352.             the pixel size. Added two new routines, GDPixelSize and GDType,
  353.             that return the pixelSize and gdType (i.e. fixedType, clutType, or 
  354.             directType) of the device. Completely rewrote GDClutSize.
  355. 4/16/93    dgp    Streamlined GDClutSize() to make it fast enough for routine use.
  356. 4/19/93    dgp    Added GDNewLinearColorTable.
  357. 4/25/93    dgp    Made CntrlParam automatic instead of static.
  358. 4/25/93    dgp    Added GDColors(device) and GDPrintGammaTable(). Alphabetized the lists
  359.             of functions in the documentation above.
  360. 5/11/93    dgp    Changed GDPrintGammaTable() to accept a simple file pointer instead of
  361.             an array of two file pointers.
  362. 7/7/93    dgp enhanced GDDacSize() to return 8 unless the gamma table is present and
  363.             reasonable.
  364. 7/29/94    dgp    added GDNameStr().
  365. 9/5/94 dgp removed assumption in printf's that int==short.
  366. 10/25/94 dgp check for missing driver, (*device)->gdRefNum==0, and treat that case
  367.             just like  device==NULL. Devices created by NewGWorld have no driver.
  368. */
  369.  
  370. #include "VideoToolbox.h"
  371. #include <assert.h>
  372. #include <ROMDefs.h>
  373. #define dRAMBased 0x0040
  374.  
  375. typedef struct VDFlagRec {
  376.     unsigned char flag;
  377.     char pad;
  378. } VDFlagRec;
  379.  
  380. typedef struct VDDefModeRec{
  381.     unsigned char spID;
  382.     char pad;
  383. } VDDefModeRec;
  384.  
  385. typedef struct {
  386.     short flags;
  387.     short blanks[3];
  388.     short open;
  389.     short prime;
  390.     short control;
  391.     short status;
  392.     short close;
  393.     Str255 name;
  394. } VideoDriver;
  395. VideoDriver *GDDriverAddress(GDHandle device);
  396.  
  397. typedef struct {
  398.     short csCode;        // control code
  399.     short length;        // total parameter block bytes
  400.     char param[];        // control call data
  401. } ScrnCtl;
  402.  
  403. typedef struct {
  404.     short spDrvrHw;        // Slot Manager ID
  405.     short slot;            // Number of slot
  406.     long dCtlDevBase;    // Start of device's address space
  407.     short mode;            // screen characteristics
  408.     short flagMask;        // Which flag bits are used
  409.     short flags;        // device state: bit 0 = 0 = mono; bit 0 = 1 = color;
  410.                         // bit 11 =  1 = startup device; bit 15 = 1 = active
  411.     short colorTable;    // 'clut' id, default = -1
  412.     short gammaTable;    // Selects color intensity, default (MacII) = -1
  413.     Rect globalRect;    // global rectangle, main device topLeft = 0,0
  414.     short ctlCount;        // total control calls
  415.     ScrnCtl ctl;
  416. } Scrn;
  417.  
  418. typedef struct {
  419.     short scrnCount;    //Total devices
  420.     Scrn scrn;
  421. } Scrns;                // 'scrn' resource
  422.  
  423. Scrn **GDGetScrn(GDHandle device);
  424.  
  425. OSErr GDSetPageDrawn(GDHandle device,short page)
  426. // Select a page of video memory to draw into.
  427. // Untested.
  428. {
  429.     int error;
  430.     short flags;
  431.     Ptr baseAddr;
  432.     static long qD=-1;
  433.  
  434.     if(qD==-1)Gestalt(gestaltQuickdrawVersion,&qD);
  435.     error=GDGetPageBase(device,page,&baseAddr);
  436.     if(!error){
  437.         if(qD>=gestalt32BitQD){
  438.             flags=GetPixelsState((**device).gdPMap);
  439.             LockPixels((**device).gdPMap);
  440.         }
  441.         (**(**device).gdPMap).baseAddr=baseAddr;
  442.         if(qD>=gestalt32BitQD){
  443.             GDeviceChanged(device);
  444.             SetPixelsState((**device).gdPMap,flags);
  445.         }
  446.     }
  447.     return error;
  448. }
  449.  
  450. OSErr GDSetPageShown(GDHandle device,short page)
  451. // Select a page of video memory for display.
  452. // Untested.
  453. {
  454.     int error;
  455.     short mode;
  456.  
  457.     error=GDGetMode(device,&mode,NULL,NULL);
  458.     if(error)return error;
  459.     return GDSetMode(device,mode,page,NULL);
  460. }
  461.  
  462. short GDType(GDHandle device)
  463. // Returns what would normally be in (**device).gdType, for occasions when
  464. // the GDevice record might be invalid because you called GDSetMode().
  465. {
  466.     int error;
  467.     static ColorSpec white={255,0xffff,0xffff,0xffff},black={0,0,0,0};
  468.  
  469.     error=GDSetEntries(device,0,0,&white);
  470.     if(!error)return clutType;
  471.     error=GDDirectSetEntries(device,0,0,&black);
  472.     if(!error)return directType;
  473.     return fixedType;
  474. }
  475.  
  476. short GDPixelSize(GDHandle device)
  477. // Returns what would normally be in (**(**device).gdPMap).pixelSize, for occasions
  478. // when the GDevice record might be invalid because you called GDSetMode().
  479. {
  480.     int error;
  481.     short mode;
  482.  
  483.     error=GDGetMode(device,&mode,NULL,NULL);
  484.     return GDModePixelSize(device,mode);
  485. }
  486.  
  487. short GDModePixelSize(GDHandle device,short mode)
  488. // Returns the pixelSize associated with a given video mode.
  489. // If you've changed the mode by calling GDSetMode and you're running a System 
  490. // older than 6.0.5 the answer may may be wrong for some of the newer video cards,
  491. // because they have ideosynchratic associations of video mode and pixel size.
  492. {
  493.     short j;
  494.     long version;
  495.  
  496.     if(mode==(**device).gdMode)return (**(**device).gdPMap).pixelSize;
  497.     Gestalt(gestaltSystemVersion,&version);
  498.     if(version>=0x605){            // Need new Palette Manager for reliable answer.
  499.         for(j=5;j>=0;j--)if(mode==HasDepth(device,1<<j,0,0))return 1<<j;
  500.     } else return 1<<(mode&7);    // Unreliable.
  501.     return 0;
  502. }
  503.  
  504. Boolean GDHasMode(GDHandle device,short mode,short *pixelSizePtr,short *pagesPtr);
  505.  
  506. Boolean GDHasMode(GDHandle device,short mode,short *pixelSizePtr,short *pagesPtr)
  507. // Returns 0 if no such mode, returns 1 if mode is available.
  508. // If pixelSizePtr is not NULL, then sets *pixelSizePtr to pixelSize or -1 if unknown.
  509. // If pagesPtr is not NULL, then sets *pagesPtr to pages.
  510. {
  511.     short pixelSize,i,hasDepthWorks,pages;
  512.     int error;
  513.     long system;
  514.     
  515.     Gestalt(gestaltSystemVersion,&system);
  516.     // On Mac IIci, Sys 6.07, HasDepth returns "mode" of 0x100 at all legal depths.
  517.     hasDepthWorks= system>=0x605     // New Palette Manager.
  518.         && HasDepth(device,(**(**device).gdPMap).pixelSize,0,0)==(**device).gdMode;
  519.     if(hasDepthWorks){
  520.         for(i=0;i<6;i++){
  521.             pixelSize=1<<i;
  522.             if(mode!=HasDepth(device,pixelSize,0,0))continue;
  523.             if(pixelSizePtr!=NULL)*pixelSizePtr=pixelSize;
  524.             if(pagesPtr!=NULL){
  525.                 error=GDGetPageCnt(device,mode,&pages);
  526.                 if(error)pages=1;
  527.                 *pagesPtr=pages;
  528.             }
  529.             return 1;
  530.         }
  531.         return 0;
  532.     }else{
  533.         // If HasDepth doesn't work properly then we can still find out whether the
  534.         // mode is available by asking the video driver for a page count, but
  535.         // I don't know of any discreet way to find out the pixelSize.
  536.         // Calling SetDepth would work, but that would irritate the user who has to
  537.         // watch and wait. 
  538.         error=GDGetPageCnt(device,mode,&pages);
  539.         if(error)pages=0;
  540.         if(pagesPtr!=NULL)*pagesPtr=pages;
  541.         if(mode==(**device).gdMode)pixelSize=(**(**device).gdPMap).pixelSize;
  542.         else pixelSize=-1;        // Unknown.
  543.         if(pixelSizePtr!=NULL)*pixelSizePtr=pixelSize;
  544.         return (pages>0);
  545.     }
  546. }
  547.  
  548. short gdClutSizeTable[33]={0,1<<1,1<<2,0,1<<4,0,0,0,1<<8,0,0,0,0,0,0,0,1<<5
  549. ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1<<8};
  550.  
  551. long GDColors(GDHandle device)
  552. // Returns the number of colors, in the current mode.
  553. {
  554.     long colors=0;
  555.     short pixelSize;
  556.     
  557.     if(device==NULL)return 2;    // for compatibility with 1-bit QuickDraw
  558.     pixelSize=(**(**device).gdPMap).pixelSize;
  559.     if(pixelSize>=1 && pixelSize<=8)colors=1<<pixelSize;
  560.     if(pixelSize==16)colors=1L<<15;
  561.     if(pixelSize==32)colors=1L<<24;
  562.     return colors;
  563. }
  564.  
  565. short GDClutSize(GDHandle device)
  566. // Returns the number of entries in the clut, in the current mode.
  567. {
  568.     short clutSize;
  569.     
  570.     // Method 1. Estimate clut size from pixel size.
  571.     clutSize=gdClutSizeTable[(**(**device).gdPMap).pixelSize];
  572.  
  573.     #if 0
  574.     // Method 2. Measure the clut's size by trying to load it.
  575.     if(GDType(device)==directType){
  576.         int error,i;
  577.         static const ColorSpec white={255,0xffff,0xffff,0xffff},black={0,0,0,0};
  578.         ColorSpec *table;
  579.         for(clutSize=256;clutSize>1;clutSize>>=1){
  580.             table=GDNewLinearColorTable(device);
  581.             if(table==NULL)PrintfExit("GDClutSize: out of memory.\n");
  582.             error=GDDirectSetEntries(device,0,clutSize-1,table);
  583.             DisposePtr((Ptr)table);
  584.             if(!error)break;
  585.         }
  586.     }
  587.     #endif
  588.     
  589.     return clutSize;
  590. }
  591.  
  592. short GDDacSize(GDHandle device)
  593. // Figures out how many bits in the video dac. Answers for each device are cached.
  594. {
  595.     short dacSize,i;
  596.     static short dacSizeCache[MAX_SCREENS];
  597.     static GDHandle deviceCache[MAX_SCREENS];
  598.     int error;
  599.     GammaTbl *gammaTblPtr=NULL;
  600.  
  601.     for(i=0;i<MAX_SCREENS;i++)if(device==deviceCache[i])return dacSizeCache[i];
  602.     error=GDGetGamma(device,&gammaTblPtr);        // Takes 200 µs.
  603.     if(error || gammaTblPtr==NULL || gammaTblPtr->gDataWidth==0)dacSize=8;                            // Oops. Take a guess.
  604.     else dacSize=gammaTblPtr->gDataWidth;
  605.     for(i=0;i<MAX_SCREENS;i++)if(NULL==deviceCache[i]){
  606.         deviceCache[i]=device;
  607.         dacSizeCache[i]=dacSize;
  608.         break;
  609.     }
  610.     return dacSize;
  611. }
  612.  
  613. ColorSpec *GDNewLinearColorTable(GDHandle device)
  614. // Creates a default table for use when gdType==directType.
  615. {
  616.     short clutSize,i;
  617.     ColorSpec *table,*linearTable;
  618.     
  619.     clutSize=GDClutSize(device);
  620.     table=linearTable=(ColorSpec *)NewPtr(clutSize*sizeof(linearTable[0]));
  621.     if(linearTable!=NULL)for(i=0;i<clutSize;i++){
  622.         table->rgb.red=table->rgb.green=table->rgb.blue
  623.             =(0xffffL*i+clutSize/2)/(clutSize-1);
  624.         table++;
  625.     }
  626.     return linearTable;
  627. }
  628.  
  629. /*
  630. Nominally equivalent to Apple's RestoreDeviceClut(), which is only available
  631. since System 6.05. However, I find that Apple's routine sometimes does nothing,
  632. whereas this routine always works. Passing a NULL argument causes restoration of
  633. cluts of all video devices.
  634. */
  635. OSErr GDRestoreDeviceClut(GDHandle device)
  636. {
  637.     int error,lastError;
  638.     short count;
  639.  
  640.     // If NULL, then recursively call ourselves for each device.
  641.     if(device==NULL){
  642.         lastError=0;
  643.         device=GetDeviceList();
  644.         while(device!=NULL) {
  645.             if(TestDeviceAttribute(device,screenDevice))
  646.                 error=GDRestoreDeviceClut(device);
  647.             if(error)lastError=error;
  648.             device=GetNextDevice(device);
  649.         }
  650.         return lastError;
  651.     }
  652.     if(GDType(device)!=directType){
  653.         count=(**(**(**device).gdPMap).pmTable).ctSize;
  654.         error=GDSetEntries(device,0,count
  655.             ,((**(**(**device).gdPMap).pmTable)).ctTable);
  656.     } else error=GDSetGamma(device,NULL);
  657.     return error;
  658. }
  659.  
  660. GammaTbl **savedGammaTable[MAX_SCREENS];
  661.  
  662. OSErr GDSaveGamma(GDHandle device)
  663. {
  664.     GammaTbl *gamma;
  665.     int error;
  666.     long size;
  667.     int i;
  668.  
  669.     if(device==NULL || (*device)->gdType==fixedType)return 0;
  670.     i=GetScreenIndex(device);
  671.     if(i>=MAX_SCREENS)return 1;
  672.      error=GDGetGamma(device,&gamma);
  673.     if(error)return error;
  674.     size=gamma->gChanCnt*gamma->gDataCnt;
  675.     if(gamma->gDataWidth>8)size*=2;
  676.     size+=sizeof(GammaTbl)+gamma->gFormulaSize;
  677.     if(savedGammaTable[i]!=NULL){
  678.         DisposeHandle((Handle)savedGammaTable[i]);
  679.         savedGammaTable[i]=NULL;
  680.     }
  681.     error=PtrToHand(gamma,(Handle *)&savedGammaTable[i],size);
  682.     return error;
  683. }
  684.  
  685. OSErr GDRestoreGamma(GDHandle device)
  686. {
  687.     int i,error;
  688.     
  689.     if((*device)->gdType==fixedType)return 0;
  690.     i=GetScreenIndex(device);
  691.     if(i>=MAX_SCREENS || savedGammaTable[i]==NULL)return 1;
  692.     error=GDSetGamma(device,*savedGammaTable[i]);
  693.     if(error)return error;
  694.     DisposeHandle((Handle)savedGammaTable[i]);
  695.     savedGammaTable[i]=NULL;
  696.     return 0;
  697. }    
  698.  
  699. OSErr GDGetDefaultGamma(GDHandle device,GammaTbl **gammaTbl)
  700. // Looks for a default gamma table in both places that Apple says to look,
  701. // but usually comes up empty handed.
  702. {
  703.     int error,i,slot;
  704.     Scrn **scrn;
  705.     GammaTbl **gammaHandle,*gamma;
  706.     SpBlock spBlock;
  707.     Ptr ptr;
  708.  
  709.     gammaHandle=NULL;
  710.     if(CountResources('gama')>0){// Any 'gama' resources available in System file?
  711.         // Check to see if 'scrn' resource in System file specifies a 'gama' resource.
  712.         scrn=GDGetScrn(device);
  713.         if(scrn!=NULL && (**scrn).gammaTable!=-1)
  714.             gammaHandle=(GammaTbl **)GetResource('gama',(**scrn).gammaTable);
  715.         DisposeHandle((Handle)scrn);
  716.     }
  717.     if(gammaHandle!=NULL){
  718.         // Got gamma table from 'gama' resource.
  719.         gamma=(GammaTbl *)NewPtr(GetHandleSize((Handle)gammaHandle));
  720.         if(gamma==NULL)return MemError();
  721.         BlockMove(*gammaHandle,gamma,GetHandleSize((Handle)gammaHandle));
  722.         DisposeHandle((Handle)gammaHandle);
  723.     }else{
  724.         // Try to get this device's default gamma table from Slot Manager.
  725.         spBlock.spSlot=slot=GetDeviceSlot(device);
  726.         spBlock.spID=0;
  727.         spBlock.spExtDev=0;
  728.         spBlock.spTBMask=3;            // match only spCategory and spCType
  729.         spBlock.spCategory=catDisplay;
  730.         spBlock.spCType=typeVideo;    // this might be too restrictive, excludes LCD
  731.         do{
  732.             error=SNextTypeSRsrc(&spBlock);
  733.             if(error)return error;
  734.         }while(spBlock.spSlot!=slot || spBlock.spRefNum!=(**device).gdRefNum);
  735.         
  736.         if(0){
  737.             // Print sResource name
  738.             spBlock.spID=sRsrcName;
  739.             error=SGetCString(&spBlock);
  740.             printf("Slot resource “%s”\n",spBlock.spResult);
  741.             if(error)return error;
  742.             DisposePtr((Ptr)spBlock.spResult);
  743.         }
  744.         
  745.         // Look for gamma directory. Unfortunately many video devices don't have one.
  746.         spBlock.spID=sGammaDir;
  747.         error=SFindStruct(&spBlock);
  748.         if(error)return error;
  749.         
  750.         // Retrieve default gamma table
  751.         spBlock.spID=0x80;
  752.         error=SGetBlock(&spBlock);
  753.         if(error)return error;
  754.         gamma=(GammaTbl *)spBlock.spResult;
  755.         ptr=(Ptr)spBlock.spResult+6;
  756.         if(0)printf("Gamma table “%s”, %ld bytes\n",ptr,GetPtrSize((Ptr)gamma));
  757.         ptr+=strlen(ptr)+1;                // table is just past string
  758.         ptr=(Ptr)((long)(ptr+1)&~1L);    // round up to even address
  759.         i=ptr-(Ptr)gamma;
  760.         BlockMove(ptr,(Ptr)gamma,GetPtrSize((Ptr)gamma)-i);
  761.         SetPtrSize((Ptr)gamma,GetPtrSize((Ptr)gamma)-i);
  762.     }
  763.     *gammaTbl=gamma;
  764.     return 0;
  765. }
  766.  
  767. Scrn **GDGetScrn(GDHandle device)
  768. // Returns handle to a copy of the specific scrn resource for this device,
  769. // or NULL if none.
  770. {
  771.     int error=0,i,j;
  772.     Scrns **scrns;
  773.     Scrn *scrn,**scrnHandle;
  774.     ScrnCtl *ctl;
  775.     int scrnCount;
  776.     long size;
  777.  
  778.     scrns=(Scrns **)GetResource('scrn',0);
  779.     if(ResError())return NULL;
  780.     HLockHi((Handle)scrns);
  781.     scrnCount=(**scrns).scrnCount;
  782.     scrn=&(**scrns).scrn;
  783.     for(i=0;i<scrnCount;i++){
  784.         if(0 && scrn->slot==GetDeviceSlot(device)){
  785.             printf("Slot %d,",(int)scrn->slot);
  786.             printf("mode %d,",(int)scrn->mode);
  787.             printf("colorTable %d,",(int)scrn->colorTable);
  788.             printf("gammaTable %d,",(int)scrn->gammaTable);
  789.             printf("%d control calls:",(int)scrn->ctlCount);
  790.         }
  791.         ctl=&scrn->ctl;
  792.         for(j=0;j<scrn->ctlCount;j++){
  793.             if(0 && scrn->slot==GetDeviceSlot(device))printf(" %d",(int)ctl->csCode);
  794.             ctl=(ScrnCtl *)((long)(ctl+1)+ctl->length);
  795.         }
  796.         if(0 && scrn->slot==GetDeviceSlot(device))printf("\n");
  797.         size=(long)ctl-(long)scrn;
  798.         if(scrn->slot==GetDeviceSlot(device))break;
  799.         scrn=(Scrn *)ctl;
  800.     }
  801.     if(i<scrnCount){
  802.         if(error)return NULL;
  803.         scrnHandle=(Scrn **)NewHandle(size);
  804.         BlockMove(scrn,*scrnHandle,size);
  805.     }else scrnHandle=NULL;
  806.     ReleaseResource((Handle)scrns);
  807.     return scrnHandle;
  808. }
  809.  
  810. OSErr GDUncorrectedGamma(GDHandle device)
  811. /*
  812. Loads a linear gamma table into the specified video device, to defeat the
  813. driver's attempt to do gamma correction.
  814.  
  815. According to Designing Cards and Drivers, 3rd edition, passing a NULL
  816. GammaTblPtr instructs the driver to create a linear table. So you may wish to
  817. just call GDSetGamma(device,NULL) instead of calling GDUncorrectedGamma(device).
  818. However, I'm not sure how old that rule is, and don't know if all the older
  819. drivers support it. Therefore the following code first examines the current
  820. gamma table, and, if it's a plain vanilla table, as described in Designing Cards
  821. and Drivers, then we write in the desired linear table. If it's fancy (or if the
  822. driver doesn't support GDGetGamma) then we pass a NULL pointer, letting the driver
  823. create the new table. Hopefully this will work with all drivers.
  824. */
  825. {
  826.     int error,i;
  827.     GammaTbl *gamma=NULL;
  828.     char *gData;
  829.  
  830.     error=GDGetGamma(device,&gamma);
  831.     if(!error && gamma->gVersion==0 && gamma->gChanCnt==1 && gamma->gDataWidth<=8){
  832.         // Overwrite the standard table
  833.         gData = (char *)&gamma->gFormulaData+gamma->gFormulaSize;
  834.         for(i=0;i<gamma->gDataCnt;i++)gData[i]=i;
  835.     }else{
  836.         // A fancy table implies a new driver, so let it do the work.
  837.         gamma=NULL;
  838.     }
  839.     error=GDSetGamma(device,gamma);
  840.     return error;
  841. }
  842.  
  843. /* KillIO doesn't do anything, so I didn't bother to implement it. */
  844.  
  845. OSErr GDPrintGammaTable(FILE *o,GDHandle device)
  846. {
  847.     unsigned char *byte;
  848.     unsigned short *word;
  849.     int error,i,j,identity;
  850.     GammaTbl *gamma;
  851.     
  852.     if((**device).gdType==fixedType)return statusErr;
  853.     error=GDGetGamma(device,&gamma);
  854.     if(error){
  855.         fprintf(o,"GetGamma: GDGetGamma() error %d\n",error);
  856.         if(error==statusErr)
  857.             fprintf(o,"The video driver doesn't support this call.\n");
  858.         return error;
  859.     }
  860.     byte=(unsigned char *)gamma->gFormulaData+gamma->gFormulaSize;
  861.     word=(unsigned short *)byte;
  862.     identity=1;
  863.     if(gamma->gDataWidth<=8)
  864.         for(i=0;i<gamma->gDataCnt;i++)identity &= (i==byte[i]);
  865.     else
  866.         for(i=0;i<gamma->gDataCnt;i++)identity &= (i==word[i]);
  867.     if(identity){
  868.         fprintf(o,"Gamma Table: identity transformation\n");
  869.     }else{
  870.         fprintf(o,"Gamma Table:\n");
  871.         fprintf(o,"at 0x%lx,gDataWidth %d,gDataCnt %d,gVersion %d,gType %d,gFormulaSize %d,gChanCnt %d\n"
  872.             ,gamma,(int)gamma->gDataWidth,(int)gamma->gDataCnt,(int)gamma->gVersion
  873.             ,(int)gamma->gType,(int)gamma->gFormulaSize,(int)gamma->gChanCnt);
  874.         for(i=0;i<gamma->gDataCnt;i+=64) {
  875.             fprintf(o,"%3d: ",i);
  876.             if(gamma->gDataWidth<=8)
  877.                 for(j=0;j<16;j++) fprintf(o," %3u",byte[i+j]);
  878.             else
  879.                 for(j=0;j<16;j++) fprintf(o," %3u",word[i+j]);
  880.             fprintf(o,"\n");
  881.         }
  882.     }
  883.     return 0;
  884. }
  885.  
  886. OSErr GDReset(GDHandle device, short *modePtr, short *pagePtr, Ptr *baseAddrPtr)
  887. /*
  888. Initialize the video card to its startup state, usually 1 bit per pixel. Returns
  889. the parameters of that state.
  890. */
  891. {
  892.     VDPageInfo myVDPageInfo;
  893.     int error;
  894.  
  895.     if(device==NULL || (*device)->gdRefNum==0) return controlErr;
  896.     myVDPageInfo.csMode= *modePtr;
  897.     myVDPageInfo.csPage= *pagePtr;
  898.     error=GDControl((*device)->gdRefNum,cscReset,(Ptr) &myVDPageInfo);
  899.     *modePtr=myVDPageInfo.csMode;
  900.     *pagePtr=myVDPageInfo.csPage;
  901.     if(baseAddrPtr!=NULL) *baseAddrPtr=myVDPageInfo.csBaseAddr;
  902.     return error;
  903. }
  904.  
  905. OSErr GDSetMode(GDHandle device,short mode,short page,Ptr *baseAddrPtr)
  906. {
  907.     VDPageInfo myVDPageInfo;
  908.     int error;
  909.  
  910.     if(device==NULL || (*device)->gdRefNum==0) return controlErr;
  911.     myVDPageInfo.csMode=mode;
  912.     myVDPageInfo.csPage=page;
  913.     error=GDControl((*device)->gdRefNum,cscSetMode,(Ptr) &myVDPageInfo);
  914.     if(baseAddrPtr!=NULL) *baseAddrPtr=myVDPageInfo.csBaseAddr;
  915.     return error;
  916. }
  917.  
  918. OSErr GDSetEntriesByType(GDHandle device,short start,short count,ColorSpec *table)
  919. // Calls GDSetEntries or GDDirectSetEntries or nothing, as appropriate.
  920. // Assumes that the GDevice record is valid, i.e. that the user has not
  921. // called GDSetMode.
  922. {
  923.     switch((*device)->gdType){
  924.     case fixedType:
  925.         return statusErr;
  926.     case clutType:
  927.         return GDSetEntries(device,start,count,table);
  928.     case directType:
  929.         return GDDirectSetEntries(device,start,count,table);
  930.     }
  931.     return 1;
  932. }
  933.  
  934. OSErr GDSetEntriesByTypeHighPriority(GDHandle device,short start,short count
  935.     ,ColorSpec *table)
  936. {
  937.     char priority;
  938.     int error;
  939.  
  940.     priority=7;
  941.     SwapPriority(&priority);
  942.     error=GDSetEntriesByType(device,start,count,table);
  943.     SwapPriority(&priority);
  944.     return error;
  945. }
  946.  
  947. OSErr GDSetEntries(GDHandle device,short start,short count,ColorSpec *table)
  948. {
  949.     VDSetEntryRecord vDSetEntryRecord;
  950.     int error;
  951.  
  952.     if(device==NULL || (*device)->gdRefNum==0) return controlErr;
  953.     vDSetEntryRecord.csStart=start;
  954.     vDSetEntryRecord.csCount=count;
  955.     vDSetEntryRecord.csTable=table;
  956.     error=GDControl((*device)->gdRefNum,cscSetEntries,(Ptr) &vDSetEntryRecord);
  957.     return error;
  958. }
  959.  
  960. OSErr GDSetGamma(GDHandle device, GammaTbl *gamma)
  961. {
  962.     int error;
  963.     VDGammaRecord myVDGammaRecord;
  964.  
  965.     myVDGammaRecord.csGTable=(Ptr)gamma;
  966.     error=GDControl((*device)->gdRefNum,cscSetGamma,(Ptr) &myVDGammaRecord);
  967.     return error;
  968. }
  969.  
  970. OSErr GDGrayPage(GDHandle device,short page)
  971. /*
  972. Called "GrayScreen" in Designing Cards and Drivers, 3rd Ed. Fills the specified
  973. page with gray, i.e. the dithered desktop pattern. I'm not aware of any
  974. particular advantage in using this instead of FillRect().
  975.  
  976. Designing Cards and Drivers, 3rd Edition, Chapter 9, says that for direct
  977. devices, i.e. >8 bit pixels, the driver will also load a linear,
  978. gamma-corrected, gray color table.
  979.  
  980. Contrary to the documentation, version 2 (in System 6.03) of the video driver
  981. for Apple's old video card requires that one supply the current mode as well.
  982. Supplying a garbage mode screwed up the screen and soon hung the software. So
  983. this code first obtains the current mode, and then supplies it in the GrayPage
  984. Control call.
  985. */
  986. {
  987.     VDPageInfo myVDPageInfo;
  988.     int error;
  989.     /* The rest of the arguments are used soley for the bug fix */
  990.     short mode=0;        /* should be ignored, but isn't */
  991.     short actualPage;    /* ignored */
  992.     Ptr baseAddr;        /* ignored */
  993.  
  994.     if(device==NULL || (*device)->gdRefNum==0) return controlErr;
  995.     
  996.     /* Work around driver bug: get the mode */
  997.     error=GDGetMode(device,&mode,&actualPage,&baseAddr);
  998.     if(error)return error;
  999.     myVDPageInfo.csMode=mode;
  1000.     
  1001.     myVDPageInfo.csPage=page;
  1002.     error=GDControl((*device)->gdRefNum,cscGrayPage,(Ptr) &myVDPageInfo);
  1003.     return error;
  1004. }
  1005.  
  1006. OSErr GDSetGray(GDHandle device,Boolean flag)
  1007. /*
  1008. Tells the driver whether you want colors (flag==0), or prefer that all colors be 
  1009. mapped to luminance-equivalent gray tones? (flag==1).
  1010. */
  1011. {
  1012.     VDFlagRec myVDFlagRec;
  1013.     int error;
  1014.  
  1015.     if(device==NULL || (*device)->gdRefNum==0) return controlErr;
  1016.     myVDFlagRec.flag=flag;
  1017.     error=GDControl((*device)->gdRefNum,cscSetGray,(Ptr) &myVDFlagRec);
  1018.     return error;
  1019. }
  1020.  
  1021. OSErr GDSetInterrupt(GDHandle device,Boolean flag)
  1022. /*
  1023. Set flag to 1 to enable VBL interrupts of this card, or 0 to disable. 
  1024. I don't know when it's appropriate to call this.
  1025. */
  1026. {
  1027.     VDFlagRec myVDFlagRec;
  1028.     int error;
  1029.  
  1030.     if(device==NULL || (*device)->gdRefNum==0) return controlErr;
  1031.     myVDFlagRec.flag=flag;
  1032.     error=GDControl((*device)->gdRefNum,cscSetInterrupt,(Ptr) &myVDFlagRec);
  1033.     return error;
  1034. }
  1035.  
  1036. OSErr GDDirectSetEntries(GDHandle device,short start,short count,ColorSpec *table)
  1037. /*
  1038. If your pixel depth is >8 then the setEntries Control call is disabled, and you use
  1039. this instead. Except for that, GDDirectSetEntries is identical to GDSetEntries.
  1040. */
  1041. {
  1042.     VDSetEntryRecord vDSetEntryRecord;
  1043.     int error;
  1044.  
  1045.     if(device==NULL || (*device)->gdRefNum==0) return controlErr;
  1046.     vDSetEntryRecord.csStart=start;
  1047.     vDSetEntryRecord.csCount=count;
  1048.     vDSetEntryRecord.csTable=table;
  1049.     error=GDControl((*device)->gdRefNum,cscDirectSetEntries,(Ptr) &vDSetEntryRecord);
  1050.     return error;
  1051. }
  1052.  
  1053. OSErr GDSetDefaultMode(GDHandle device,short mode)
  1054. /*
  1055. Supposedly, you tell it what mode you want to start up with when you reboot.
  1056. (I've never been able to get this to work. No error and no effect. Perhaps I've
  1057. misunderstood its purpose.)
  1058. */
  1059. {
  1060.     VDDefModeRec myVDDefModeRec;
  1061.     int error;
  1062.  
  1063.     if(device==NULL || (*device)->gdRefNum==0) return controlErr;
  1064.     myVDDefModeRec.spID=mode;
  1065.     error=GDControl((*device)->gdRefNum,cscSetDefaultMode,(Ptr) &myVDDefModeRec);
  1066.     return error;
  1067. }
  1068.  
  1069. OSErr GDGetMode(GDHandle device,short *modePtr,short *pagePtr,Ptr *baseAddrPtr)
  1070. /*
  1071. It tells you the current mode, page of video memory, and the base address of that
  1072. page.
  1073. */
  1074. {
  1075.     VDPageInfo myVDPageInfo;
  1076.     int error;
  1077.  
  1078.     if(device==NULL || (*device)->gdRefNum==0) return statusErr;
  1079.     error=GDStatus((*device)->gdRefNum,cscGetMode,(Ptr) &myVDPageInfo);
  1080.     if(modePtr!=NULL)*modePtr=myVDPageInfo.csMode;
  1081.     if(pagePtr!=NULL)*pagePtr=myVDPageInfo.csPage;
  1082.     if(baseAddrPtr!=NULL) *baseAddrPtr=myVDPageInfo.csBaseAddr;
  1083.     return error;
  1084. }
  1085.  
  1086. OSErr GDGetEntries(GDHandle device,short start,short count,ColorSpec *table)
  1087. /*
  1088. This is much as you'd expect after reading GDSetEntries above. Note that unless
  1089. the gamma table is linear, the values returned may not be the same as those
  1090. originally passed by GDSetEntries. So call GDUncorrectedGamma first. Note that
  1091. version 2 (in System 6.03) of the video driver for Apple's old video card had a
  1092. bug and did not support "indexed" mode, i.e. start==-1. This is fixed in System
  1093. 6.05. Apple's .Display_Video_Apple_RBV1 v. 0 video driver (built-in to Mac IIci)
  1094. crashes if you attempt to make this call. However, that's a thing of the past,
  1095. because we now first patch the driver to fix the bug. Try the demo TimeVideo.
  1096. */
  1097. {
  1098.     VDSetEntryRecord vDSetEntryRecord;
  1099.     int error;
  1100.     static int bugsPatched=0;
  1101.     unsigned char *name;
  1102.     int version;
  1103.  
  1104.     if(device==NULL || (*device)->gdRefNum==0)return statusErr;
  1105.     if(!bugsPatched){
  1106.         PatchMacIIciVideoDriver();
  1107.         bugsPatched=1;
  1108.     }
  1109.  
  1110.     // Contrary to Apple's rules, these drivers crash if we attempt
  1111.     // a getEntries call. So let's be polite and instead simply return
  1112.     // an error message indicating that this call is not available.
  1113.     name=GDName(device);
  1114.     version=GDVersion(device);
  1115.     if(EqualString(name,"\p.Display_Video_Apple_RBV1",1,1) && version==0)return statusErr;
  1116.     if(EqualString(name,"\p.Color_Video_Display",1,1) && version==9288)return statusErr;
  1117.  
  1118.     vDSetEntryRecord.csStart=start;
  1119.     vDSetEntryRecord.csCount=count;
  1120.     vDSetEntryRecord.csTable=table;
  1121.     error=GDStatus((*device)->gdRefNum,cscGetEntries,(Ptr) &vDSetEntryRecord);
  1122.     return error;
  1123. }
  1124.  
  1125. OSErr GDGetPageCnt(GDHandle device,short mode,short *pagesPtr)
  1126. /*
  1127. Called "GetPages" in Designing Cards and Drivers, 3rd Ed. You tell it what mode
  1128. you're interested in. It tells you how many pages of video ram are available.
  1129. */
  1130. {
  1131.     VDPageInfo myVDPageInfo;
  1132.     int error;
  1133.  
  1134.     if(device==NULL || (*device)->gdRefNum==0) return statusErr;
  1135.     myVDPageInfo.csMode=mode;
  1136.     error=GDStatus((*device)->gdRefNum,cscGetPageCnt,(Ptr) &myVDPageInfo);
  1137.     if(!error)*pagesPtr=myVDPageInfo.csPage;
  1138.     return error;
  1139. }
  1140.  
  1141. OSErr GDGetPageBase(GDHandle device,short page,Ptr *baseAddrPtr)
  1142. /*
  1143. Called "GetBaseAddr" in Designing Cards and Drivers, 3rd Ed. You tell it what
  1144. page of video memory you're interested in (in the current video mode). It tells
  1145. you the base address of that page.
  1146. */
  1147. {
  1148.     VDPageInfo myVDPageInfo;
  1149.     int error;
  1150.  
  1151.     if(device==NULL || (*device)->gdRefNum==0) return statusErr;
  1152.     myVDPageInfo.csPage=page;
  1153.     error=GDStatus((*device)->gdRefNum,cscGetPageBase,(Ptr) &myVDPageInfo);
  1154.     if(baseAddrPtr!=NULL) *baseAddrPtr=myVDPageInfo.csBaseAddr;
  1155.     return error;
  1156. }
  1157.  
  1158. OSErr GDGetGray(GDHandle device,Boolean *flagPtr)
  1159. // It tells you what the flag is set to. Do you want colors? (flag==0) Or do you
  1160. // want all colors mapped to luminance-equivalent gray tones? (flag==1).
  1161. {
  1162.     VDFlagRec myVDFlagRec;
  1163.     int error;
  1164.  
  1165.     if(device==NULL || (*device)->gdRefNum==0) return statusErr;
  1166.     error=GDStatus((*device)->gdRefNum,cscGetGray,(Ptr) &myVDFlagRec);
  1167.     *flagPtr=myVDFlagRec.flag;
  1168.     return error;
  1169. }
  1170.  
  1171. OSErr GDGetInterrupt(GDHandle device,Boolean *flagPtr)
  1172. // Get flag. 1 if VBL interrupts of this card are enabled. 0 if disabled. 
  1173. {
  1174.     VDFlagRec myVDFlagRec;
  1175.     int error;
  1176.  
  1177.     if(device==NULL || (*device)->gdRefNum==0) return statusErr;
  1178.     error=GDStatus((*device)->gdRefNum,cscGetInterrupt,(Ptr) &myVDFlagRec);
  1179.     *flagPtr=myVDFlagRec.flag;
  1180.     return error;
  1181. }
  1182.  
  1183. OSErr GDGetGamma(GDHandle device,GammaTbl **myGammaTblHandle)
  1184. /*
  1185. Returns a pointer to the Gamma table in the specified video device. (I.e. you
  1186. pass it a pointer to your pointer, a handle, which it uses to load your
  1187. pointer.)
  1188.  
  1189. Note that version 2 (in System ≤6.03) of the video driver for Apple's old video
  1190. card does not support this call due to a bug in the driver code. The later
  1191. versions of the driver (3, 4, and 5, in System 6.04 and later) work correctly.
  1192. */
  1193. {
  1194.     int error;
  1195.     VDGammaRecord myVDGammaRecord;
  1196.  
  1197.     if(device==NULL || (*device)->gdRefNum==0) return statusErr;
  1198.     myVDGammaRecord.csGTable=NULL;    // default address is NULL
  1199.     error=GDStatus((*device)->gdRefNum,cscGetGamma,(Ptr) &myVDGammaRecord);
  1200.     *myGammaTblHandle=(GammaTblPtr)myVDGammaRecord.csGTable;
  1201.     return error;
  1202. }
  1203.  
  1204. OSErr GDGetDefaultMode(GDHandle device,short *modePtr)
  1205. // It tells you what the default mode is. I'm not sure what this means.
  1206. {
  1207.     VDDefModeRec myVDDefModeRec;
  1208.     int error;
  1209.  
  1210.     if(device==NULL || (*device)->gdRefNum==0) return statusErr;
  1211.     error=GDStatus((*device)->gdRefNum,cscGetDefaultMode,(Ptr) &myVDDefModeRec);
  1212.     *modePtr=(unsigned char)myVDDefModeRec.spID;
  1213.     return error;
  1214. }
  1215.  
  1216. OSErr GDControl(int refNum,int csCode,Ptr csParamPtr)
  1217. // Uses low-level PBControl() call to implement a "Control()" call that works! 
  1218. // I don't know why this wasn't discussed in Apple Tech Note 262.
  1219. {
  1220.     CntrlParam control;
  1221.     int error;
  1222.     
  1223.     control.ioCompletion=NULL;
  1224.     control.ioCRefNum=refNum;
  1225.     control.csCode=csCode;
  1226.     *((Ptr *) &control.csParam[0]) = csParamPtr;
  1227.     error=PBControl((ParmBlkPtr) &control,0);
  1228.     return error;
  1229. }
  1230.  
  1231. OSErr GDStatus(int refNum,int csCode,Ptr csParamPtr)
  1232. // Uses low-level PBStatus() call to implement a "Status()" call that works! The
  1233. // need for this is explained in Apple Tech Note 262, which was issued in response
  1234. // to my bug report in summer of '89.
  1235. {
  1236.     CntrlParam control;
  1237.     int error;
  1238.     
  1239.     control.ioCompletion=NULL;
  1240.     control.ioCRefNum=refNum;
  1241.     control.csCode=csCode;
  1242.     *((Ptr *) &control.csParam[0]) = csParamPtr;
  1243.     error=PBStatus((ParmBlkPtr) &control,0);
  1244.     return error;
  1245. }
  1246.  
  1247. #if 0
  1248. /*
  1249. From: absurd@apple.apple.com (Tim Dierks, software saboteur)
  1250. Date: Fri, 20 Nov 1992 00:38:14 GMT
  1251. Organization: MacDTS Marauders
  1252.  
  1253. Here's the right way to get the slot number of a monitor, given its
  1254. GDevice:  (as a bonus, this also gets the name of the card in
  1255. question; to just get the slot, chop off all the lines after the
  1256. *slot = ... line.
  1257. */
  1258.  
  1259. OSErr GetSlotAndName(GDHandle gDev,short *slot,char *name)
  1260. {   OSErr       err;
  1261.     short       refNum;
  1262.     SpBlock     spb;
  1263.     
  1264.     refNum = (**gDev).gdRefNum;    // video driver refNum for this GDevice
  1265.     *slot = (**(AuxDCEHandle)GetDCtlEntry(refNum)).dCtlSlot;
  1266.                                    // slot in which this video card sits
  1267.     spb.spSlot = *slot;         // In the slot we're interested in
  1268.     spb.spID = 0;
  1269.     spb.spExtDev = 0;
  1270.     spb.spCategory = 1;         // Get the board sResource
  1271.     spb.spCType = 0;
  1272.     spb.spDrvrSW = 0;
  1273.     spb.spDrvrHW = 0;
  1274.     spb.spTBMask = 0;
  1275.     err = SNextTypeSRsrc(&spb);
  1276.     if (err)return err;
  1277.     spb.spID = 2;               // Getting the description string
  1278.                                 // spSPointer was set up by SNextTypesResource
  1279.     err = SGetCString(&spb);
  1280.     if (err)return err;
  1281.     strcpy(name,(char *)spb.spResult);  // Get the returned string
  1282.     DisposPtr((Ptr)spb.spResult);    // Undocumented; we have to dispose of it
  1283.     c2pstr(name);
  1284.     return noErr;
  1285. }
  1286. #endif
  1287.  
  1288. char *GDCardName(GDHandle device)
  1289. /*
  1290. Returns the address of a C string containing the name of the video card. You
  1291. should call DisposPtr() on the returned address when you no longer need it.
  1292. Takes about 1.5 ms; I don't know why Apple's slot routines are so slow. This
  1293. routine sets up the flags in the SNextTypeSRsrc() call quite differently from
  1294. the above example, but I don't know if that matters, since I've been using this
  1295. routine for a year or so without any problems.
  1296. */
  1297. {
  1298.     AuxDCE **auxDCEHandle;
  1299.     SpBlock SPB,SPB1;
  1300.     
  1301.     if(device==NULL || (*device)->gdRefNum==0)return "";
  1302.     auxDCEHandle = (AuxDCE **) GetDCtlEntry((*device)->gdRefNum);
  1303.     SPB.spSlot = (**auxDCEHandle).dCtlSlot;
  1304.     SPB.spCategory = 0;
  1305.     SPB.spCType =     0;
  1306.     SPB.spDrvrSW =     0;
  1307.     SPB.spDrvrHW =     1;
  1308.     SPB.spTBMask = 0xf;
  1309.     SPB.spID = 0;
  1310.     SPB.spExtDev = 0;
  1311.     if(SNextTypeSRsrc(&SPB) == noErr) {
  1312.         SPB1.spsPointer = SPB.spsPointer;
  1313.         SPB1.spID = 2;
  1314.         SGetCString(&SPB1);
  1315.         return (char *)SPB1.spResult;
  1316.     }
  1317.     else return "";
  1318. }
  1319.  
  1320. unsigned char *GDName(GDHandle device)
  1321. // Returns a pointer to the name of the driver (a pascal string). 
  1322. {
  1323.     VideoDriver *videoDriverPtr;
  1324.  
  1325.     if(device==NULL || (*device)->gdRefNum==0)return "\p";
  1326.     videoDriverPtr=GDDriverAddress(device);
  1327.     return videoDriverPtr->name;
  1328. }
  1329.  
  1330. char *GDNameStr(GDHandle device)
  1331. // Returns the driver name as a C string.
  1332. {
  1333.     unsigned char *sp;
  1334.     static char name[32];
  1335.     
  1336.     sp=GDName(device);
  1337.     memcpy(name,sp,sp[0]+1);
  1338.     return p2cstr((unsigned char *)name);
  1339. }
  1340.  
  1341.  
  1342. int GDVersion(GDHandle device)
  1343. // Returns the version number of the driver. From the first word-aligned word after
  1344. // the name string.
  1345. {
  1346.     int version;
  1347.     unsigned char *bytePtr;
  1348.  
  1349.     if(device==NULL || (*device)->gdRefNum==0)return 0;
  1350.     bytePtr=GDName(device);
  1351.     bytePtr += 1+bytePtr[0];    /* go to end of Pascal string */
  1352.     bytePtr = (unsigned char *)((long)(bytePtr+1) & ~1);    // round up to word boundary
  1353.     version = *(short *)bytePtr;
  1354.     return version;
  1355. }
  1356.  
  1357. VideoDriver *GDDriverAddress(GDHandle device)
  1358. // Returns a pointer to the driver, whether in ROM or RAM. 
  1359. {
  1360.     AuxDCE **auxDCEHandle;
  1361.     VideoDriver *videoDriverPtr;
  1362.  
  1363.     if(device==NULL || (*device)->gdRefNum==0)return NULL;
  1364.     auxDCEHandle = (AuxDCE **) GetDCtlEntry((*device)->gdRefNum);
  1365.     if((**auxDCEHandle).dCtlFlags & dRAMBased){
  1366.         /* RAM-based driver. */
  1367.         videoDriverPtr=*(VideoDriver **) (**auxDCEHandle).dCtlDriver;
  1368.     }
  1369.     else{
  1370.         /* ROM-based driver. */
  1371.         videoDriverPtr=(VideoDriver *) (**auxDCEHandle).dCtlDriver;
  1372.     }
  1373.     return videoDriverPtr;
  1374. }
  1375.  
  1376. /*
  1377. ROUTINE: PatchMacIIciVideoDriver
  1378. PURPOSE:
  1379. It is unlikely that you will need to call this explicitly, because it is called
  1380. automatically by GDGetEntries the first time it is invoked, and the sole purpose
  1381. of this routine is to fix a driver bug that would cause a crash when called by
  1382. GDGetEntries.
  1383.  
  1384. The Mac IIci built-in video driver (.Display_Video_Apple_RBV1 driver, version 0)
  1385. has a bug that causes it to crash if you try to do a getEntries Status request.
  1386. PatchMacIIciVideoDriver() will find and patch the memory-resident copy of the
  1387. buggy driver. Only two instructions are modified, to save and restore more
  1388. registers. This fix persists only until the next reboot. If the patch is
  1389. successfully applied the version number is increased from 0 to 100, to
  1390. distinguish it from versions 0 and 1.
  1391.  
  1392. A returned value of 1 indicates success: the needed patch was applied, either now
  1393. or previously. A return value of 0 indicates no patch was needed.
  1394.  
  1395. This patch is based on a comparison of the version 0 and 1 drivers used by the
  1396. Mac IIci and IIsi, respectively. The patch changes a pair of save and restore
  1397. operations (MOVEM.L to and from the stack) to save and restore registers D1 and
  1398. A1 as well as D4, A3, and A4. There are many other differences between versions
  1399. 0 and 1 of the driver, but this change is enough to keep the version 0 driver
  1400. from crashing when we attempt to read the clut by calling GDGetEntries.
  1401.  
  1402. The only change that the Mac operating system could possibly notice is that,
  1403. when we're done patching, we set the handle to be non-purgeable, since purging
  1404. and reloading the driver would eliminate the patch.
  1405.  
  1406. edward_de_Jong@bmug.org and Robert Savoy (SAVOY@RISVAX.ROWLAND.ORG) reported
  1407. that their Mac IIci computers' built-in video driver is ROM-based, so
  1408. PatchMacIIciVideoDriver was enhanced to deal with a ROM-based driver, by copying
  1409. the driver into RAM, making that the active driver, and patching it. Robert
  1410. Savoy reports that it works fine.
  1411.  
  1412. An alternative, permanent, solution is described in the file "Video synch":
  1413. upgrading to Apple's bug-free version 1 of the driver. However, that solution
  1414. only works if the Mac IIci has more than one monitor.
  1415.  
  1416. */
  1417.  
  1418. int PatchMacIIciVideoDriver(void)
  1419. {
  1420.     GDHandle device;
  1421.     short *w;
  1422.     AuxDCE **auxDCEHandle;
  1423.     Handle handle;
  1424.     enum{badVersion=0};
  1425.     int error;
  1426.     long value;
  1427.     
  1428.     error=Gestalt(gestaltQuickdrawVersion,&value);
  1429.     if(error || value<gestalt8BitQD)return 0;    // need 8-bit quickdraw
  1430.     device = GetDeviceList();
  1431.     while(1) {
  1432.         if(device==NULL || (*device)->gdRefNum==0)return 0;
  1433.         if (!TestDeviceAttribute(device,screenDevice)
  1434.             || !TestDeviceAttribute(device,screenActive)){
  1435.             device=GetNextDevice(device);
  1436.             continue;
  1437.         }
  1438.         if(EqualString("\p.Display_Video_Apple_RBV1",GDName(device),1,1))
  1439.             switch(GDVersion(device)){
  1440.                 case badVersion:
  1441.                     break;
  1442.                 case badVersion+100:    // already patched
  1443.                     return 1;
  1444.                 default:
  1445.                     return 0;
  1446.         }else{
  1447.             device=GetNextDevice(device);
  1448.             continue;
  1449.         }
  1450.         break;
  1451.     }
  1452.     auxDCEHandle = (AuxDCE **) GetDCtlEntry((*device)->gdRefNum);
  1453.     
  1454.     // Move ROM-based driver into RAM.
  1455.     if(!((**auxDCEHandle).dCtlFlags & dRAMBased)){
  1456.         long bytes;
  1457.         VideoDriver *driver;
  1458.         
  1459.         driver=(VideoDriver *)(**auxDCEHandle).dCtlDriver;
  1460.         // Sometimes the word preceding the driver in ROM seems to be the
  1461.         // driver size, but not always, e.g. the built-in driver on the Mac IIsi.
  1462.         bytes=*((short *)driver-1);
  1463.         // Driver size unknown, guessing (generously) at twice the highest offset.
  1464.         bytes=driver->open;
  1465.         if(bytes<driver->prime)bytes=driver->prime;
  1466.         if(bytes<driver->control)bytes=driver->control;
  1467.         if(bytes<driver->status)bytes=driver->status;
  1468.         if(bytes<driver->close)bytes=driver->close;
  1469.         bytes*=2;
  1470.         // We know the Mac IIci driver size to be 1896
  1471.         // when ROM version is 124 rev. 1, but who knows for later ROMs?
  1472.         //bytes=1896;
  1473.         handle=NewHandleSys(bytes);
  1474.         if(handle==NULL)return 0;    // Insufficient room on System heap.
  1475.         HLockHi(handle);
  1476.         BlockMove((Ptr)driver,*handle,bytes);
  1477.         (**auxDCEHandle).dCtlDriver=(Ptr)handle;
  1478.         (**auxDCEHandle).dCtlFlags |= dRAMBased;        
  1479.     }
  1480.     
  1481.     // Patch RAM-based driver.
  1482.     handle=(Handle)(**auxDCEHandle).dCtlDriver;
  1483.     w=*(short **)handle;
  1484.     if(w[0x51e/2]!=0x818 || w[0x590/2]!=0x1810 || w[0x2c/2]!=badVersion){
  1485.         printf("PatchMacIIciVideoDriver error.\n");
  1486.         return 0;
  1487.     }
  1488.     w[0x51e/2]=0x4858;
  1489.     w[0x590/2]=0x1a12;
  1490.     w[0x2c/2]+=100;                                    // Change version number.
  1491.     if(w[0x51e/2]!=0x4858 || w[0x590/2]!=0x1a12){
  1492.         printf("PatchMacIIciVideoDriver error.\n");
  1493.         return 0;
  1494.     }
  1495.     HNoPurge(handle);
  1496.     return 1;
  1497. }
  1498.